package pt.inescid.components.jobmanager;
import pt.inescid.components.messages.Task;

import rice.p2p.commonapi.Id;

/**
 * 
 * Task Info contains the necessary information to retrieve a task-result
 * 
 * @author Filipe Paredes
 * @author Sergio Esteves
 */
public class TaskInfo implements Comparable {  
  
  // Original Task
  private Task task;
  // Time limit to try and get the gridlet result
  private long timeLimit;
  // Key to use in the Past lookup for this gridlet
  private Id lookupKey;
  // Proximity from the node to the first hop
  private long maxDelay;
  // Number of retries to get the result
  private int retriesLeft;
  
  
  public TaskInfo(Task task, long timeLimit, 
          Id lookupKey, long maxDelay, int retries) {
    this.task = task;
    this.timeLimit = timeLimit;
    this.lookupKey = lookupKey;
    this.maxDelay = maxDelay;
    this.retriesLeft = retries;
  }
  
  public Task getGridlet(){
      return task;
  }
  
  public long getTimeLimit() {
    return timeLimit;
  }
  
  public Id getLookupKey() {
    return lookupKey;
  }
  
  public long getMaxDelay() {
    return maxDelay;
  }
  
  public int getRetriesLeft() {
    return retriesLeft;
  }
  
  // (retriesLeft + 1) => excludes the first try that does't count as retry
//  public int getRetriesUsed() {
    //return (int) (gm.NUM_RETRIES - retriesLeft);
//	  return 0;
//  }
  
  // It's necessary to set a new timeLimit and reduce the number of retries
  public void oneAttemptUsed(long now) {
	  //FIXME
    //timeLimit = now + (gm.AVG_PROXIMITY * gm.AVG_HOPS);
    retriesLeft--;
  }
  
  public int compareTo(Object arg0) {
    TaskInfo gi = (TaskInfo)arg0;
    if (gi == this) return 0;
    return (int)(timeLimit-gi.getTimeLimit());
  }
  
  /**
   * A descriptive toString()
   */
  public String toString() {
    return "TaskInfo: [" + task + ", " + timeLimit + "]" + " key = " + lookupKey;
  }
}